<template>
<div>
  <div class="table-data">
    <div class="search-box">
      <!-- <el-input size="small" v-model="searchMap.name" placeholder="类型名称"></el-input>
      <el-button size="small" type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button> -->
      <el-button size="small" @click="handleAdd()"><i class="el-icon-plus"></i><span>添加类型</span></el-button>
    </div>
    <el-table 
      v-loading="loading"
      element-loading-text="正在加载"
      element-loading-spinner="el-icon-loading" :data="tableData" :header-cell-style="headClass" style="width:100%" :height="tHeight" class="table-box">
      <el-table-column label="id" prop="id" width="120"></el-table-column>
      <el-table-column label="类型名称" prop="name"></el-table-column>
      <el-table-column label="包含商家数量" prop="store_count"></el-table-column>
      <el-table-column label="备注" prop="desc"></el-table-column>
      <el-table-column label="创建时间" prop="create_time"></el-table-column>
      <el-table-column label="操作" width="160">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="handleEdit(scope.$index,scope.row)">编辑</el-button>
          <el-button v-if="scope.row.store_count==0" size="mini" type="danger" @click="handleRemove(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pages" ref="page-box">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[10,30,50]"
        :page-size="size"
        layout="total,sizes,prev,pager,next,jumper"
        :total="total"
      ></el-pagination>
    </div>
    <!-- <AddDialog :isAdd="isAdd" @closeDialog="closeDialog" @loadData="loadData"></AddDialog> -->
    <EditDialog :isEdit="isEdit" :isAdd="isAdd" :form="formData" @closeDialog="closeDialog" @loadData="loadData"></EditDialog>
  </div>
    <router-view></router-view>
  </div>
</template>

<script lang="ts">

import { Component, Vue, Provide,Prop,Watch } from "vue-property-decorator";
// import { State, Getter, Mutation, Action } from "vuex-class";

import EditDialog from "./EditDialog.vue";
import businessApi from "@/api/business";

@Component({
  components: {EditDialog }
})
export default class businessTypeList extends Vue {
  // @Getter("user") getUser: any;
  @Provide() tHeight: number = document.body.offsetHeight - 323;
  @Provide() tableData: any = []; // 表格数据
  @Provide() multipleTable: any = []; // 表格数据
  @Provide() page: number = 1; // 当前page
  @Provide() size: number = 10; // 请求数据的个数 默认5
  @Provide() total: number = 0; // 总数据条数
  @Provide() isAdd: Boolean = false; // 是否展示添加弹窗
  @Provide() isEdit: Boolean = false; // 是否展示编辑弹窗
  @Provide() loading: Boolean = true; // 是否显示加载图标
  @Provide() advertisers: any = [];
  @Provide() searchData: any = {
    pageNum: 1
  };

  @Provide() formData: any = {
    id:"",
    name:"",
    desc: "",
  };
  @Provide() searchMap:{
    name: string,
    phone:string,
  }={
    name:'',
    phone:''
  }
  headClass() {
    return "text-align: center;background:#eef1f6;";
  }
  handleRemove( data) {
    this.$confirm('此操作将永久删除----'+data.name+'----分类,是否继续？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      closeOnClickModal:false
    }).then(() => {
      businessApi.removeBusinessType({id:data.id})
      .then((res: any) => {
        if(res.data.status){
          this.$message({
            message: '操作成功',
            type: "success"
          });
          this.loadData();
        }
      });
    }).catch(() => {
      this.$message({
        type: 'info',
        message: '已取消删除'
      });          
    });
    
  }
 // 新增
  handleAdd(){
    this.isAdd=true;
    this.isEdit=true;
    this.formData={id:"",name:"",desc: "",}
  }
  // 编辑
  handleEdit(index: number, row: any){
    this.isEdit=true;
    this.formData.name=row.name
    this.formData.desc=row.desc
    this.formData.id=row.id
  }
  // 关闭弹窗
  closeDialog() {
    this.isAdd = false;
    this.isEdit = false;
    this.loadData();
  }

  created() {
    this.loadData();
  }

  loadData() {
    businessApi.businessTypeList(this.page,this.size)
      .then((res: any) => {
        this.tableData = res.data.data.list;
        this.total = res.data.data.total;
        this.loading=false
      });
  }

  handleSizeChange(val: number): void {
    this.size = val;
    this.page = 1;
    this.loadData();
  }

  handleCurrentChange(val: number): void {
    this.page = val;
    this.loadData();
  }

  handleSearch(): void {
    // 点击搜索
    this.page = 1;
    this.loadData();
  }
}
</script>

<style lang="scss" scoped>
.el-range-editor.el-input__inner{
  margin-right: 10px
}
</style>
